package com.definesys.smecrailtransit.module.lineCharts.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.ItemErrorWBinding;
import com.definesys.smecrailtransit.databinding.ItemErrorYBinding;
import com.definesys.smecrailtransit.module.lineCharts.beans.EleErrorMsg;
import com.definesys.smecrailtransit.module.lineCharts.beans.YesReport;
import com.definesys.smecrailtransit.BR;
import com.definesys.smecrailtransit.utils.CommonUtils;

import java.util.ArrayList;

/**
 * Created by xupeizuo on 2018/3/7.
 */

public class SmecErrorAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {


    private Context mContext;

    private int errorType=0;
    private ArrayList<EleErrorMsg> errorEleList=new ArrayList<>();
    private ArrayList<YesReport> faultEleList=new ArrayList<>();

    public SmecErrorAdapter(Context mContext,int type) {
        this.mContext = mContext;
        this.errorType=type;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(errorType == 0){
            ItemErrorWBinding itemErrorWBinding= DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_error_w,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(itemErrorWBinding.getRoot());
            smecBaseRecyclerViewHolder.setBinding(itemErrorWBinding);
            smecBaseRecyclerViewHolder.setViewType(viewType);
            return smecBaseRecyclerViewHolder;
        }
        if(errorType == 1){
            ItemErrorYBinding itemErrorYBinding= DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.item_error_y,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(itemErrorYBinding.getRoot());
            smecBaseRecyclerViewHolder.setBinding(itemErrorYBinding);
            smecBaseRecyclerViewHolder.setViewType(viewType);
            return smecBaseRecyclerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
       if(errorType == 0){
           ItemErrorWBinding itemErrorWBinding=(ItemErrorWBinding) holder.getBinding();
           itemErrorWBinding.setVariable(BR.errorW,errorEleList.get(position));
       }
        if(errorType == 1){
            ItemErrorYBinding itemErrorYBinding=(ItemErrorYBinding)holder.getBinding();
            itemErrorYBinding.setVariable(BR.yes,faultEleList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return errorType == 0 ? errorEleList.size() : faultEleList.size();
    }

    public void setErrorEleList(ArrayList<EleErrorMsg> errorEleList) {
        if(CommonUtils.notEmpty(errorEleList)){
            this.errorEleList = errorEleList;
            notifyDataSetChanged();
        }
    }

    public void setFaultEleList(ArrayList<YesReport> faultEleList) {
        if(CommonUtils.notEmpty(faultEleList)){
            this.faultEleList = faultEleList;
            notifyDataSetChanged();
        }
    }
}
